package com.ezcoding.controller;

import com.ezcoding.service.CompanyService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.tx.Tx;

@Before(Tx.class)
public class CompanyController extends Controller {

    /**
     * 创建企业
     */
    public void addCompany() {
        String username = getPara("username");
        String companyName = getPara("companyName");
        String contact = getPara("contact");

        renderJson(CompanyService.getInstance().addCompany(username, companyName, contact));
    }

    /**
     * 更新企业信息
     */
    public void updateCompany() {
        String username = getPara("username");
        String companyId = getPara("companyId");
        String taxRate = getPara("taxRate");

        String companyName = getPara("companyName");
        String contact = getPara("contact");
        String tel = getPara("tel");
        String fax = getPara("fax");
        String website = getPara("website");
        String email = getPara("email");
        String province = getPara("province");
        String city = getPara("city");
        String district = getPara("district");
        String address = getPara("address");
        String account = getPara("account");
        String introduction = getPara("introduction");

        String ticketCompanyName = getPara("ticketCompanyName");
        String taxNum = getPara("taxNum");
        String bank = getPara("bank");
        String ticketAccount = getPara("ticketAccount");
        String ticketTel = getPara("ticketTel");
        String ticketAddress = getPara("ticketAddress");

        renderJson(CompanyService.getInstance().updateCompany(
                username,
                companyId,
                taxRate,
                companyName,
                contact,
                tel,
                fax,
                website,
                email,
                province,
                city,
                district,
                address,
                account,
                introduction,
                ticketCompanyName,
                taxNum,
                bank,
                ticketAccount,
                ticketTel,
                ticketAddress));
    }

    /**
     * 通过企业ID查询企业
     */
    public void queryCompany() {
        String username = getPara("username");
        String companyId = getPara("companyId");

        renderJson(CompanyService.getInstance().queryCompany(username, companyId, "查询成功！"));
    }

    /**
     * 加入企业
     */
    public void joinCompany() {
        String username = getPara("username");
        String companyId = getPara("companyId");

        renderJson(CompanyService.getInstance().joinCompany(username, companyId));
    }

    /**
     * 更新企业类别
     */
    public void updateCategory() {
        String categoryType = getPara("categoryType");
        String companyId = getPara("companyId");
        String category = getPara("category");

        renderJson(CompanyService.getInstance().updateCategory(categoryType, companyId, category));
    }

    /**
     * 更新目录项
     */
    public void updateItemCategory() {
        String categoryType = getPara("categoryType");
        String username = getPara("username");
        String companyId = getPara("companyId");
        String oldPrimaryDirectory = getPara("oldPrimaryDirectory");
        String oldSecondaryDirectory = getPara("oldSecondaryDirectory");
        String newPrimaryDirectory = getPara("newPrimaryDirectory");
        String newSecondaryDirectory = getPara("newSecondaryDirectory");

        renderJson(CompanyService.getInstance().updateItemCategory(
                categoryType,
                username,
                companyId,
                oldPrimaryDirectory,
                oldSecondaryDirectory,
                newPrimaryDirectory,
                newSecondaryDirectory));
    }

    /**
     * 删除目录项
     */
    public void deleteItemCategory() {
        String categoryType = getPara("categoryType");
        String username = getPara("username");
        String companyId = getPara("companyId");
        String primaryDirectory = getPara("primaryDirectory");
        String secondaryDirectory = getPara("secondaryDirectory");

        renderJson(CompanyService.getInstance().deleteItemCategory(categoryType, username, companyId, primaryDirectory, secondaryDirectory));
    }

    /**
     * 刷新企业家底数据
     */
    public void updateCompanyData() {
        String username = getPara("username");
        String companyId = getPara("companyId");

        renderJson(CompanyService.getInstance().queryCompany(username, companyId, "刷新成功！"));
    }

    /**
     * 盘点家底信息，发票盘点和资金盘点
     */
    public void homeCount() {
        //type 0:发票盘点 1:资金盘点
        int type = getParaToInt("type");
        String username = getPara("username");
        String companyId = getPara("companyId");
        String oldValue = getPara("oldValue");
        String countValue = getPara("countValue");

        renderJson(CompanyService.getInstance().homeCount(type, username, companyId, oldValue, countValue));
    }
}
